home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / objam01.lha / objam / objc / list.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  2.3 KB  |  126 lines

  1. /*
  2. ** ObjectiveAmiga: Generic single linked list to keep various information
  3. ** See GNU:lib/libobjam/ReadMe for details
  4. */
  5.  
  6.  
  7. #include <objc/objc.h>
  8.  
  9.  
  10. struct objc_list {
  11.   void *head;
  12.   struct objc_list *tail;
  13. };
  14.  
  15. /* Return a cons cell produced from (head . tail) */
  16.  
  17. static inline struct objc_list* 
  18. list_cons(void* head, struct objc_list* tail)
  19. {
  20.   struct objc_list* cell;
  21.  
  22.   cell = (struct objc_list*)__objc_xmalloc(sizeof(struct objc_list));
  23.   cell->head = head;
  24.   cell->tail = tail;
  25.   return cell;
  26. }
  27.  
  28. /* Return the length of a list, list_length(NULL) returns zero */
  29.  
  30. static inline int
  31. list_length(struct objc_list* list)
  32. {
  33.   int i = 0;
  34.   while(list)
  35.     {
  36.       i += 1;
  37.       list = list->tail;
  38.     }
  39.   return i;
  40. }
  41.  
  42. /* Return the Nth element of LIST, where N count from zero.  If N 
  43.    larger than the list length, NULL is returned  */
  44.  
  45. static inline void*
  46. list_nth(int index, struct objc_list* list)
  47. {
  48.   while(index-- != 0)
  49.     {
  50.       if(list->tail)
  51.     list = list->tail;
  52.       else
  53.     return 0;
  54.     }
  55.   return list->head;
  56. }
  57.  
  58. /* Remove the element at the head by replacing it by its successor */
  59.  
  60. static inline void
  61. list_remove_head(struct objc_list** list)
  62. {
  63.   if ((*list)->tail)
  64.     {
  65.       struct objc_list* tail = (*list)->tail; /* fetch next */
  66.       *(*list) = *tail;        /* copy next to list head */
  67.       __objc_xfree(tail);            /* free next */
  68.     }
  69.   else                /* only one element in list */
  70.     {
  71.       __objc_xfree (*list);
  72.       (*list) = 0;
  73.     }
  74. }
  75.  
  76.  
  77. /* Remove the element with `car' set to ELEMENT */
  78.  
  79. static inline void
  80. list_remove_elem(struct objc_list** list, void* elem)
  81. {
  82.   while (*list) {
  83.     if ((*list)->head == elem)
  84.       list_remove_head(list);
  85.     list = &((*list)->tail);
  86.   }
  87. }
  88.  
  89. /* Map FUNCTION over all elements in LIST */
  90.  
  91. static inline void
  92. list_mapcar(struct objc_list* list, void(*function)(void*))
  93. {
  94.   while(list)
  95.     {
  96.       (*function)(list->head);
  97.       list = list->tail;
  98.     }
  99. }
  100.  
  101. /* Return element that has ELEM as car */
  102.  
  103. static inline struct objc_list**
  104. list_find(struct objc_list** list, void* elem)
  105. {
  106.   while(*list)
  107.     {
  108.     if ((*list)->head == elem)
  109.       return list;
  110.     list = &((*list)->tail);
  111.     }
  112.   return NULL;
  113. }
  114.  
  115. /* Free list (backwards recursive) */
  116.  
  117. static void
  118. list_free(struct objc_list* list)
  119. {
  120.   if(list)
  121.     {
  122.       list_free(list->tail);
  123.       __objc_xfree(list);
  124.     }
  125. }
  126.